package com.developer.platform.domain.listener;

import com.developer.platform.domain.Group;
import com.developer.platform.domain.QuartzJob;
import com.developer.platform.domain.Template;
import com.developer.platform.domain.event.NoticeEvent;
import com.developer.platform.domain.event.NoticeEventPublisher;
import com.developer.platform.domain.service.IQuartzJobService;
import com.developer.platform.infrastructure.data.mapper.GroupMapper;
import com.developer.platform.infrastructure.data.mapper.TemplateMapper;
import com.google.common.collect.Maps;
import com.google.common.eventbus.Subscribe;
import com.veeker.core.utils.DateUtils;
import com.veeker.core.utils.JsonUtils;
import java.util.Calendar;
import java.util.Map;
import java.util.Objects;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-12-29
 */
@Component
public class NoticeEventListener {

  private final GroupMapper groupMapper;
  private final TemplateMapper templateMapper;
  private final IQuartzJobService quartzJobService;

  public NoticeEventListener(NoticeEventPublisher noticeEventPublisher,
      GroupMapper groupMapper,
      TemplateMapper templateMapper,
      IQuartzJobService quartzJobService){
    this.groupMapper = groupMapper;
    this.templateMapper = templateMapper;
    this.quartzJobService = quartzJobService;
    noticeEventPublisher.register(this);
  }

  @Subscribe
  public void asyncPublishUserEnabled(NoticeEvent noticeEvent){
    //  创建群通知定时任务
    this.pushQuartzJob(noticeEvent);
  }

  private void pushQuartzJob(NoticeEvent noticeEvent){
    Group group = groupMapper.selectById(noticeEvent.getGroupId());
    if(Objects.isNull(group)){
      return;
    }
    Template template = templateMapper.selectById(noticeEvent.getTemplateId());
    if(Objects.isNull(template)){
      return;
    }

    if(Objects.isNull(noticeEvent.getPushTime())){
      noticeEvent.setPushTime(DateUtils.getAddDate(noticeEvent.getCurredTime(),Calendar.SECOND,2));
    }

    quartzJobService.save(new QuartzJob(
        group.getName() + "-" + template.getName(), "系统更新",
        "com.developer.platform.domain.scheduler.NoticeOnlineQuartzJob",
        DateUtils.formatDate(DateUtils.getAddDate(noticeEvent.getOnlineTime(),
            Calendar.SECOND,2),"ss mm HH dd MM ?"),
        noticeEvent.getStatus(), JsonUtils.toJson(jobData(noticeEvent, group, template, "系统更新"))));

    quartzJobService.save(new QuartzJob(
        group.getName() + "-" + template.getName(), "群通知",
        "com.developer.platform.domain.scheduler.NoticeSchedulerQuartzJob",
        DateUtils.formatDate(noticeEvent.getPushTime(),"ss mm HH dd MM ?"),
        noticeEvent.getStatus(), JsonUtils.toJson(jobData(noticeEvent, group, template, "群通知"))));
  }


  private Map<String, Object> jobData(NoticeEvent noticeEvent, Group group, Template template,
      String subgroup){
    Map<String, Object> map = Maps.newHashMap();
    map.put("id", noticeEvent.getId());
    map.put("templateName",template.getName());
    map.put("templateContent",template.getContent());
    map.put("jobName",group.getName() + "-" + template.getName());
    map.put("subgroup",subgroup);
    map.put("webhook",group.getWebhook());
    map.put("sign",group.getSign());
    return map;
  }

}
